<?php
/**
 * PHP 时间处理函数
 * 修改编码格式:统一编码：UTF-8
 * @author Tujt 2017-05-15
 */
class PHPSession {
    public function  __construct(){
    	#codeing
    }
	/**
	 * 开启session
	 * @author Tujt 2017-05-15
	 */
	public function start() {
	    static $static_session_start;
        if(1 != $static_session_start){
            session_start();
        }
		$static_session_start = 1;
	}
    /**
     * 加密字符串
     * @author Toby.tu 2016-12-16
     */
    public function pwd($key=''){
        if(empty($key)) return '';
        $label = config_item('session_key');
        $label .= 'phpsession_key';
        return md5(sha1($key.$label).sha1($label.$key));
    }
    /**
     * 存入session
     * @author Toby.tu 2017-05-15
     */
    public function set($key='',$data='$$_cache_&&_data_##',$time=60){
    	if(empty($key)) return false;//没有键值
        if('$$_cache_&&_data_##' == $data) return false;//空
        if(!empty($data)) $val = serialize($data);
        else $val = '';
        $key = $this->pwd($key);//对键值进行加密，防止重复
        $this->start();//开启session
        $_SESSION[$key] = $val;
    }
    /**
     * 取session
     * @author Toby.tu 2017-05-15
     */
    public function get($key){
    	if(empty($key)) return array();
        static $phpredis_session_getdata;//增加静态变量，同一个缓存不多次读取
        $key = $this->pwd($key);//对键值进行加密，防止重复
        if(isset($phpredis_session_getdata[$key])){//缓存已经读取过一次
            return $phpredis_session_getdata[$key];
        }
        $data = '';
        $this->start();//开启session
        $cachedata = $_SESSION[$key];
        if(!empty($cachedata)) $data = unserialize($cachedata);
        return $phpredis_session_getdata[$key] = $data;
    }
    /**
     * 删除session
     * @author Toby.tu 2017-05-15
     */
    public function rm($key=''){
        if(!empty($key)){
            $key = $this->pwd($key);//对键值进行加密，防止重复
	    	$this->start();//开启session
			if($_SESSION[$key]){
				$_SESSION[$key] = array();
				unset($_SESSION[$key]);
			}
        }
    }
}
?>
